package com.example.element.module.lifecycle.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.base.AbsActivity;
import com.example.element.R;
import com.example.element.Utils.RouterControl;
import com.example.element.databinding.ActFragmentLifecycleBinding;
import com.example.element.module.lifecycle.fragment.LifecycleOne;
import com.example.element.module.lifecycle.fragment.LifecycleThree;
import com.example.element.module.lifecycle.fragment.LifecycleTwo;

@Route(path = RouterControl.FRAGMENT_LIFECYCLE)
public class FragLifecycleActivity extends AbsActivity<ActFragmentLifecycleBinding> {

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_fragment_lifecycle;
    }

    @Override
    protected void main() {
        initFragment();
//        initViewPager();
    }

//    private void initViewPager() {
//        List<Fragment> fragments = new ArrayList<>();
//        fragments.add(LifecycleOne.getInstance());
//        fragments.add(LifecycleTwo.getInstance());
//        fragments.add(LifecycleThree.getInstance());
//        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
//        mBinding.vpContainer.setOffscreenPageLimit(fragments.size());
//        mBinding.vpContainer.setAdapter(adapter);
//
//    }

    @Override
    protected void initListener() {
        mBinding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_one) {
                replaceRedioButton(0);
            } else if (checkedId == R.id.rb_two) {
                replaceRedioButton(1);
            } else if (checkedId == R.id.rb_three) {
                replaceRedioButton(2);
            }
        });

//        mBinding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
//            if (checkedId == R.id.rb_one) {
//                mBinding.vpContainer.setCurrentItem(0);
//            } else if (checkedId == R.id.rb_two) {
//                mBinding.vpContainer.setCurrentItem(1);
//            } else if (checkedId == R.id.rb_three) {
//                mBinding.vpContainer.setCurrentItem(2);
//
//            }
//        });

    }

    private void initFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        LifecycleOne fragmentOne = LifecycleOne.getInstance();
        fragmentTransaction.add(R.id.fl_container, fragmentOne);
        fragmentTransaction.commit();
    }

    private void replaceRedioButton(int position) {
        mBinding.rbOne.setBackgroundColor(getResources().getColor(R.color.gray2));
        mBinding.rbTwo.setBackgroundColor(getResources().getColor(R.color.gray2));
        mBinding.rbThree.setBackgroundColor(getResources().getColor(R.color.gray2));

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            case 0:
                fragmentTransaction.replace(R.id.fl_container, LifecycleOne.getInstance());
                mBinding.rbOne.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 1:
                fragmentTransaction.replace(R.id.fl_container, LifecycleTwo.getInstance());
                mBinding.rbTwo.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 2:
                fragmentTransaction.replace(R.id.fl_container, LifecycleThree.getInstance());
                mBinding.rbThree.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                break;
        }
        fragmentTransaction.commit();
    }


}
